﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DataModel;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.ModelInterface;
using QLBH.ModelFactory;

namespace QLBH.Category
{
    public partial class AddKhachHangDlg : Form
    {
        public eDlgType DlgType { set; get; }
        public IKhachHangModel Model { set; get; }
        
        public AddKhachHangDlg()
        {
            InitializeComponent();

            Model = SaleProductModelFactory.GetKhachHangModelInstance();
            Model.KHID = Guid.NewGuid();
            Model.KHCODE = CodeHelper.GetNewCode();

            Model.CREATEDDATE = DateTime.Now;
        }

        private void AddKhachHangDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                //Ctrl+S
                btnSave_Click(sender, e);
            }
        }
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                try
                {
                    UpdateModelFromControl();
                    if (DlgType == eDlgType.ADD)
                    {
                        KHACHHANG entity = Model.ToEntity();
                        entity = KhachHangController.GetInstance().Add(entity);
                        Model.InitFromEntity(entity);
                    }
                    else
                    {
                        KHACHHANG entity = Model.ToEntity();
                        KhachHangController.GetInstance().Edit(entity);
                    }
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "QL Khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private void UpdateModelToControl()
        {
            tbxMaNCC.Text = Model.KHCODE;
            tbxTenNCC.Text = Model.TENKH;
            dtpNgaySinh.Value = (DateTime)Model.NGAYSINH;
            cmbGioiTinh.SelectedText = Model.GIOITINH;
            chbKHTN.Checked = (bool)Model.ISKHTIEMNANG;

            tbxMaSoThue.Text = Model.MASOTHUE;
            tbxDiDong.Text = Model.DIDONG;
            tbxDienThoai.Text = Model.DIENTHOAI;
            tbxEmail.Text = Model.EMAIL;

            
            rtbGhiChu.Text = Model.GHICHU;
            rtbDiaChi.Text = Model.DIACHI;
            
            dtpNgayTao.Value = (DateTime)Model.CREATEDDATE;

            tbxMaNCC.Focus();
            tbxMaNCC.SelectAll();
            
        }
        private void UpdateModelFromControl()
        {
            Model.KHCODE = tbxMaNCC.Text;
            Model.TENKH = tbxTenNCC.Text;
            Model.MASOTHUE = tbxMaSoThue.Text;
            Model.CREATEDDATE = dtpNgayTao.Value;
            Model.ISKHTIEMNANG = chbKHTN.Checked;

            Model.GIOITINH = cmbGioiTinh.SelectedText;
            Model.DIACHI = rtbDiaChi.Text;
            Model.DIDONG = tbxDiDong.Text;
            Model.EMAIL = tbxEmail.Text;

            Model.GHICHU = rtbGhiChu.Text;
            Model.NGAYSINH = dtpNgaySinh.Value;           
            Model.DIENTHOAI = tbxDienThoai.Text;
        }
        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxMaNCC.Text, "Mã KH", 14);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxMaNCC.SelectAll();
                tbxMaNCC.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(tbxTenNCC.Text, "Tên KH", 50);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxTenNCC.SelectAll();
                tbxTenNCC.Focus();
                return false;
            }            
            try
            {
                DataInputHelper.CheckEmailData(tbxEmail.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(tbxDienThoai.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxDienThoai.SelectAll();
                tbxDienThoai.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(tbxDiDong.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxDiDong.SelectAll();
                tbxDiDong.Focus();
                return false;
            }            
            try
            {
                DataInputHelper.CheckDateTime((DateTime)dtpNgayTao.Value, "Ngày tạo");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpNgayTao.Select();
                dtpNgayTao.Focus();
                return false;
            }
            return true;
        }

        private void AddKhachHangDlg_Load(object sender, EventArgs e)
        {
            if (DlgType == eDlgType.ADD)
            {
                this.Text = "Thêm mới khách hàng";

                UpdateModelToControl();
            }
            else
            {
                this.Text = "Sửa thông tin khách hàng";

                UpdateModelToControl();

                dtpNgayTao.Enabled = false;
            }
        }

    }
}
